﻿using Dlpad.Services;
using Dto;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace Dlpad.Controllers
{
    //字典数据CRUD
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class DictDataController : ControllerBase
    {
        private readonly IDictDataService _dictDataService;

        public DictDataController(IDictDataService dictDataService)
        {
            _dictDataService = dictDataService;
        }

        /// <summary>
        /// 分页查询字典数据
        /// </summary>
        [HttpGet]
        public async Task<ActionResult<PagedResultDto<DictDataResponseDto>>> GetDictDataList([FromQuery] DictDataQueryDto query)
        {
            try
            {
                var result = await _dictDataService.GetDictDataListAsync(query);
                return Ok(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取字典数据列表失败: {ex.Message}");
                return StatusCode(500, new { error = "获取字典数据列表失败" });
            }
        }

        /// <summary>
        /// 根据ID获取字典数据
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ActionResult<SysDictData>> GetDictData(long id)
        {
            try
            {
                var dictData = await _dictDataService.GetDictDataByIdAsync(id);
                if (dictData == null)
                {
                    return NotFound();
                }
                return Ok(dictData);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"获取字典数据失败: {ex.Message}");
                return StatusCode(500, new { error = "获取字典数据失败" });
            }
        }

        /// <summary>
        /// 创建字典数据
        /// </summary>
        [HttpPost]
        public async Task<ActionResult> CreateDictData(SysDictData dictData)
        {
            try
            {
                // 设置创建和更新信息
                dictData.CreateBy = "admin";
                dictData.CreateTime = DateTime.Now;
                dictData.UpdateBy = "admin";
                dictData.UpdateTime = DateTime.Now;

                if (await _dictDataService.CreateDictDataAsync(dictData))
                {
                    return CreatedAtAction(nameof(GetDictData), new { id = dictData.DictCode }, dictData);
                }

                return BadRequest("创建失败");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"创建字典数据失败: {ex.Message}");              
                return BadRequest($"创建失败：{ex.Message}"); 
            }
        }

        /// <summary>
        /// 更新字典数据
        /// </summary>
        [HttpPut("{id}")]
        public async Task<ActionResult> UpdateDictData(long id, SysDictData dictData)
        {
            try
            {
                if (id != dictData.DictCode)
                {
                    return BadRequest("ID不匹配");
                }

                var existingDictData = await _dictDataService.GetDictDataByIdAsync(id);
                if (existingDictData == null)
                {
                    return NotFound("字典数据不存在");
                }

                // 保留原有的创建信息
                dictData.CreateBy = existingDictData.CreateBy;
                dictData.CreateTime = existingDictData.CreateTime;
                dictData.UpdateBy = "admin";
                dictData.UpdateTime = DateTime.Now;

                var updateResult = await _dictDataService.UpdateDictDataAsync(dictData);
                if (updateResult)
                {
                    return NoContent();
                }
                else
                {
                    return BadRequest("更新失败");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"更新字典数据失败: {ex.Message}");
                return BadRequest($"更新失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 删除字典数据
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteDictData(long id)
        {
            try
            {
                if (await _dictDataService.DeleteDictDataAsync(id))
                {
                    return NoContent();
                }

                return BadRequest("删除失败");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"删除字典数据失败: {ex.Message}");
                return BadRequest($"删除失败：{ex.Message}");
            }
        }
    }
}
